<?php
namespace app\admin\controller;

use app\common\model\Users;
use app\common\util\AdminException;
use think\facade\Db;
use think\facade\View;

class Index extends Base
{
    public function index()
    {
        $today = strtotime(date("Y-m-d"));
        $count['send_order'] = Db::name('order')->where("order_status=1")->count();//待发货订单
        $count['new_order'] = Db::name('order')->whereDay("add_time")->count();//今日新增订单
        $count['order'] = Db::name('order')->where("1=1")->count();//订单总数
        $count['ry_send_order'] = Db::name('foster_order')->where("order_status=1")->count();//认养中订单
        $count['ry_order'] = Db::name('foster_order')->where("1=1")->count();//认养订单
        $count['dh_send_order'] = Db::name('convert_order')->where("order_status=1")->count();//兑换待发货订单
        $count['dh_new_order'] = Db::name('convert_order')->whereDay("add_time")->count();//今日新增兑换订单
        $count['dh_order'] = Db::name('convert_order')->where("1=1")->count();//兑换订单总数
        $count['jf_send_order'] = Db::name('jifen_order')->where("order_status=1")->count();//积分待发货订单
        $count['jf_new_order'] = Db::name('jifen_order')->whereDay("add_time")->count();//今日新增积分订单
        $count['jf_order'] = Db::name('jifen_order')->where("1=1")->count();//积分订单总数
        $count['jh_send_order'] = Db::name('stock_order')->where("order_status=1")->count();//进货待发货订单
        $count['jh_new_order'] = Db::name('stock_order')->whereDay("add_time")->count();//今日新增进货订单
        $count['jh_order'] = Db::name('stock_order')->where("1=1")->count();//进货订单总数
        $count['check_with'] = Db::name('withdrawals')->where("status=-1")->count();//待处理提现
        $count['check_store'] = Db::name('store')->where("status=-1")->count();//待审核商家数
        $count['store'] = Db::name('store')->where("1=1")->count();//商家总数
        $count['check_pick'] = Db::name('pick')->where("status=-1")->count();//待审核自提点数
        $count['pick'] = Db::name('pick')->where("1=1")->count();//自提点总数
        $count['with'] = Db::name('withdrawals')->where("status=1")->count();//提现成功总数
        $count['with_sum'] = Db::name('withdrawals')->where("status=1")->sum('money');//提现成功总额
        $count['goods'] =  Db::name('goods')->where("1=1")->count();//商品总数
        $count['goods_category'] =  Db::name('goods_category')->where("1=1")->count();//商品分类总数
        $count['goods_type'] =  Db::name('goods_type')->where("1=1")->count();//商品模型总数
        $count['convert_goods'] =  Db::name('convert_goods')->where("1=1")->count();//兑换商品总数
        $count['jifen_goods'] =  Db::name('jifen_goods')->where("1=1")->count();//积分商品总数
        $count['stock_goods'] =  Db::name('stock_goods')->where("1=1")->count();//进货商品总数
        $count['foster_goods_category'] =  Db::name('foster_goods_category')->where("1=1")->count();//认养分类总数
        $count['foster_goods'] =  Db::name('foster_goods')->where("1=1")->count();//认养商品总数
        $count['monitor_class'] =  Db::name('monitor_class')->where("1=1")->count();//监控分类总数
        $count['monitors'] =  Db::name('monitors')->where("1=1")->count();//监控总数
        $count['user_wallet_deal'] =  Db::name('user_wallet_deal')->where("1=1")->count();//交易总数
        $count['jyz_user_wallet_deal'] =  Db::name('user_wallet_deal')->where("status=-1")->count();//交易中总数
        $count['ad'] = Db::name('ad')->where("1=1")->count();//广告数量
        $count['user_level'] = Db::name('user_level')->where("1=1")->count();//等级数量
        $count['wallet'] = Db::name('wallet')->where("1=1")->count();//钱包数量
        $count['given'] = Db::name('given')->where("1=1")->count();//转赠数量
        $count['user_wallet_given'] = Db::name('user_wallet_given')->where("1=1")->count();//转让数量
        $count['users'] = Db::name('users')->where("1=1")->count();//会员总数
        $count['is_lock'] = Db::name('users')->where("is_lock=1")->count();//冻结会员总数
        $count['today_login'] = Db::name('users')->where("last_login>=$today")->count();//今日访问
        $count['new_users'] = Db::name('users')->where("reg_time>=$today")->count();//新增会员
        $count['user_money'] = Db::name('users')->where("1=1")->sum('user_money');//粮票总额
        $count['jifen'] = Db::name('users')->where("1=1")->sum('jifen');//积分总额
        $count['mcbt'] = Db::name('users')->where("1=1")->sum('mcbt');//牧场补贴总额
        $count['reserve_money'] = Db::name('users')->where("1=1")->sum('reserve_money');//储备金总额
        //统计每个级别会员数量
//        $user_level_list=Db::name('user_level')->field('level_id,name')->select();
//        foreach ($user_level_list as $key => $val){
//            $level_count=Db::name('users')->where('level',$val['level_id'])->count();
//            $user_level_list[$key]['level_count']=$level_count;
//        }
        $users=Db::name('users')->group('reg_time1')->field("count(*) as num,FROM_UNIXTIME(reg_time,'%Y-%m-%d') as reg_time1")->select();
        foreach ($users as $val){
            $arr[$val['reg_time1']] = $val['num'];
        }
        $begin = date('Y-m-d', strtotime("-1 month"));//1个月前
        $end = date('Y-m-d', strtotime('+1 days'));
        $start_time=strtotime($begin);
        $end_time=strtotime($end);
        for($i=$start_time;$i<=$end_time;$i=$i+24*3600){
            $brr[] = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
            $day1[] = date('Y-m-d',$i);
        }
        $result = array('data'=>$brr,'time'=>$day1);
        $orders = Db::name("order")
            ->field(" COUNT(*) as tnum,sum(order_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap ")
            ->where(" add_time >$start_time and add_time < $end_time  and order_status>0")
            ->group('gap')
            ->select();
        foreach ($orders as $val1){
            $crr[$val1['gap']] = $val1['tnum'];
            $drr[$val1['gap']] = $val1['amount'];
        }
        for($i=$start_time;$i<=$end_time;$i=$i+24*3600){
            $tmp_num = empty($crr[date('Y-m-d',$i)]) ? 0 : $crr[date('Y-m-d',$i)];
            $tmp_amount = empty($drr[date('Y-m-d',$i)]) ? 0 : $drr[date('Y-m-d',$i)];
            $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
            $order_arr[] = $tmp_num;
            $amount_arr[] = $tmp_amount;
            $sign_arr[] = $tmp_sign;
            $date = date('Y-m-d',$i);
            $day[] = $date;
        }
        $order_result = ['order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day];
        View::assign('order_result',json_encode($order_result));
        View::assign('result',json_encode($result));
        View::assign('count', $count);
        return view();
    }
    //修改数据表某个字段的值
    public function changeTableVal(){
        //数据表名称
        $table=input('table');
        //主键名
        $pk=Db::name($table)->getPk();
        if (empty($pk)){
            throw new AdminException('主键获取失败');
        }
        //字段名
        $field=input('field');
        //id
        $ids=input('ids/a');
        //要修改的值
        $value=input('value');
        $res = Db::name($table)->whereIn($pk,$ids)->update([$field => $value]);
        if ($res){
            throw new AdminException('操作成功',1);
        }else{
            throw new AdminException('操作失败');
        }
    }
    //公共删除方法
    public function public_delete(){
        //数据表名称
        $table=input('table');
        if (empty($table)){
            throw new AdminException('缺少数据表名称');
        }
        //主键名
        $pk=Db::name($table)->getPk();
        if (empty($pk)){
            throw new AdminException('主键获取失败');
        }
        //id
        $ids=input('ids/a');
        if (empty($ids)){
            throw new AdminException('请选择数据');
        }
        $res = Db::name($table)->whereIn($pk,$ids)->delete();
        if ($res){
            throw new AdminException('操作成功',1);
        }else{
            throw new AdminException('操作失败');
        }
    }
    //获取下级分类
    public function get_category(){
        $parent_id = input('parent_id/d'); // 商品分类 父id
        $list = Db::name('goods_category')->where("parent_id", $parent_id)->select();
        if ($list) {
            throw new AdminException('获取成功',1,['result' => $list]);
        }
        throw new AdminException('获取失败');
    }
    //获取下级地区
    public function get_region(){
        $parent_id = input('parent_id'); // 商品分类 父id
        $list = Db::name('region')->where('parent_id', $parent_id)->column('id,name');
        if ($list) {
            throw new AdminException('获取成功',1,['result' => $list]);
        }
        throw new AdminException('获取失败');
    }
}
